<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="APP名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入APP名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户端" prop="clientType">
        <el-select v-model="queryParams.clientType" placeholder="请选择客户端" clearable>
          <el-option v-for="i in clientTypeList" :key="i.id" :label="i.fullName" :value="i.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="版本更新时间" prop="dateRange" label-width="120px">
        <el-date-picker v-model="dateRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']"
          clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['manager:version:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manager:version:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manager:version:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="APP名称" align="center" prop="appName" />
      <el-table-column label="APP类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.type | dynamicText(typeOptions) }}
        </template>
      </el-table-column>
      <el-table-column label="客户端类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.clientType | dynamicText(clientTypeList) }}
        </template>
      </el-table-column>
      <el-table-column label="是否强制更新" align="center" prop="forceUpdate">
        <template slot-scope="scope">
          {{ scope.row.forceUpdate == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="版本名称" align="center" prop="versionName" />
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="版本更新时间" align="center" prop="versionUpdateDate" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:version:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manager:version:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改APP版本对话框 -->
    <el-dialog :title="`${form.id ? '修改' : '新增'}APP版本`" :visible.sync="open" width="600px" :close-on-click-modal="false"
      append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="APP名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="App类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio v-for="i in typeOptions" :key="i.id" :label="i.id">
              {{ i.fullName }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="客户端" prop="clientType">
          <el-radio-group v-model="form.clientType">
            <el-radio v-for="i in clientTypeList" :key="i.id" :label="i.id">
              {{ i.fullName }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="版本名称" prop="versionName">
          <el-input v-model="form.versionName" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入更新内容" />
        </el-form-item>
        <el-form-item label="版本更新时间" prop="versionUpdateDate">
          <el-date-picker clearable v-model="form.versionUpdateDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%" placeholder="请选择版本更新时间" />
        </el-form-item>
        <el-form-item label="是否强制更新" prop="forceUpdate">
          <el-switch v-model="form.forceUpdate" active-color="#13ce66" inactive-color="" :active-value="1"
            :inactive-value="0" />
        </el-form-item>
        <el-form-item  prop="downloadUrl">
          <template #label>
            <el-popover
              placement="left"
              width="175"
              trigger="hover"
              :disabled="!form.downloadUrl"
              @show="showQrCode"
              @hide="hideQrCode"
            >
              <div id="qrCode" ref="qrCode" />
              <span slot="reference">文件上传</span>
            </el-popover>
          </template>
          <File-upload v-model="form.downloadUrl" :limit="1" :file-size="100" :is-show-tip="false"
            :file-type="['apk', 'ipa']" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVersion,
  getVersion,
  delVersion,
  addVersion,
  updateVersion,
} from "@/api/manager/version";
import QRCode from 'qrcodejs2';
export default {
  name: "Version",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // APP版本表格数据
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientType: null,
        appName: null,
        startTime: null,
        endTime: null,
      },
      clientTypeList: [
        { fullName: "维保端", id: 1 },
        { fullName: "业主端", id: 2 },
        { fullName: "维修端", id: 3 },
      ],
      // 表单参数
      form: {
        forceUpdate: 0,
      },
      // 表单校验
      rules: {
        appName: [{ required: true, message: "APP名称不能为空", trigger: "blur" }],
        versionName: [{ required: true, message: "版本名称不能为空", trigger: "blur" }],
        version: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        versionUpdateDate: [
          { required: true, message: "版本更新日期时间为空", trigger: "blur" },
        ],
        downloadUrl: [{ required: true, message: "下载地址不能为空", trigger: "blur" }],
      },
      typeOptions: [
        { fullName: "安卓", id: "ANDROID" },
        { fullName: "苹果", id: "IOS" },
      ],
      // 时间参数
      dateRange: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询APP版本列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appName: null,
        content: null,
        downloadUrl: null,
        forceUpdate: 0,
        clientType: 1, //  客户端类型（1维保 2业主 3维修）
        type: "ANDROID",
        versionName: null,
        version: null,
        versionUpdateDate: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange && this.dateRange.length) {
        this.queryParams.startTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVersion(id).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    // 加载二维码
    showQrCode() {
      if (this.form.downloadUrl) {
        new QRCode(this.$refs.qrCode, {
          width: 150,
          height: 150,
          text: process.env.VUE_APP_WEBSC_API + this.form.downloadUrl,
          correctLevel: QRCode.CorrectLevel.H,
        });
      }
    },
    // 隐藏二维码
    hideQrCode() {
      this.$refs.qrCode.innerHTML = "";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateVersion(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVersion(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除APP版本编号为"' + ids + '"的数据项？')
        .then(() => {
          delVersion(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => { });
    },
  },
};
</script>
